{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "211a7f63",
   "metadata": {},
   "source": [
    "### 列表的基本办法（列表的私有方法）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54c41eb9",
   "metadata": {},
   "source": [
    "### 1.  增加 :\n",
    "## append() 增加在list的末尾"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "cd80ad99",
   "metadata": {},
   "outputs": [],
   "source": [
    "# anthor:Mo Xiufang\n",
    "# 时间：10.24 week06笔记\n",
    "names = [\"Mo Xiufang\", \"Liang Zherong\", \"Xu Zhichao\", \"Xie Yiming\"]\n",
    "stu_id = [1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "75ea0ab6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "append之后的names: ['Mo Xiufang', 'Liang Zherong', 'Xu Zhichao', 'Xie Yiming', 'Song Ge']\n"
     ]
    }
   ],
   "source": [
    "names.append(\"Song Ge\")\n",
    "print(\"append之后的names:\", names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ceb1baee",
   "metadata": {},
   "source": [
    "## 2.  插入insert() 可以指定位置增加元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e68768a3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "insert之后的names： ['Mo Xiufang', 'Liang Zherong', '超级无敌大帅哥', 'Xu Zhichao', 'Xie Yiming', 'Song Ge']\n"
     ]
    }
   ],
   "source": [
    "names.insert(2, \"超级无敌大帅哥\")\n",
    "print(\"insert之后的names：\", names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6eb709f4",
   "metadata": {},
   "source": [
    "## 3. 删除的方法： 删除 remove() 和pop()\n",
    "区别：\n",
    "### 如果删除list中的多个元素时，remove比较合适；pop可删除特定位置的元素\n",
    "             \n",
    "###             remove()  [一般情况下，使用remove先用in做判断]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "5e2a61fe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "remove删除的结果： [1001, 1002, 1003, 1005, 1006, 1007, 1008, 1009]\n"
     ]
    }
   ],
   "source": [
    "if 1004 in stu_id:\n",
    "    stu_id.remove(1004)\n",
    "print(\"remove删除的结果：\", stu_id)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "01f68f9d",
   "metadata": {},
   "source": [
    "###   pop() 删除位置为3的这个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "d8b76c50",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pop()方法后的结果: [1001, 1002, 1003, 1006, 1007, 1008, 1009]\n"
     ]
    }
   ],
   "source": [
    "stu_id.pop(3)\n",
    "print(\"pop()方法后的结果:\", stu_id)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c5f67b9",
   "metadata": {},
   "source": [
    "### 4. index()索引 查看该元素如1002 在stu_id中第一个出现的位置索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "b1dcd36f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "print(stu_id.index(1002))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de746647",
   "metadata": {},
   "source": [
    "### 5. clear()清除所有数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "f5b93610",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "清除names列表后的值： []\n"
     ]
    }
   ],
   "source": [
    "names.clear()\n",
    "print(\"清除names列表后的值：\", names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3555d3d6",
   "metadata": {},
   "source": [
    "### 6. count()查看列表元素出现的个数,(简单应用）统计分数出现的次数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "9f60ec0a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "count方法的结果： 1\n",
      "[1001, 1002, 1003, 1006, 1007, 1008, 1009]\n"
     ]
    }
   ],
   "source": [
    "print(\"count方法的结果：\", stu_id.count(1001))\n",
    "print(stu_id)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e4edc4d",
   "metadata": {},
   "source": [
    "### 7. extend() 是给指定的list进行扩充"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "2f5cf240",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1001, 1002, 1003, 1006, 1007, 1008, 1009]\n"
     ]
    }
   ],
   "source": [
    "names.extend(stu_id)\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d920bf2",
   "metadata": {},
   "source": [
    "### 8.列表的其他方法（Sequence Types 的一些方法）\n",
    "### len() [查看list长度]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "f318a3db",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "names的长度/names的列表元素个数： 7\n"
     ]
    }
   ],
   "source": [
    "print(\"names的长度/names的列表元素个数：\",len(names))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe3e454a",
   "metadata": {},
   "source": [
    "### in [查看元素是否存在list当中，结果为布尔值，False/True,一般在操作List的修改和删除时会先判断]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "6d4a7685",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "print(108 in names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "68b84298",
   "metadata": {},
   "source": [
    "## 知识点_列表高级方法_枚举"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2989a890",
   "metadata": {},
   "source": [
    "###   index和枚举：index只能查找列表中相同元素的第一个元素，而枚举可以找出所有重复的元素以及他们所在的位置\n",
    "###  index()索引 查看该元素如1001 在stu_id中第一个出现的位置索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "83fd1e9a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "0 1001\n",
      "1 1002\n",
      "2 1003\n",
      "3 1004\n",
      "4 1005\n",
      "5 1006\n",
      "6 1007\n",
      "7 1008\n",
      "8 1009\n"
     ]
    }
   ],
   "source": [
    "stu_id = [1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009]\n",
    "# 目的：取出所有位置1001的index\n",
    "for i in stu_id:\n",
    "    # 循环遍历stu_id的所有内容\n",
    "    if i == 1001:\n",
    "        # 判断如果元素为1001的时候,怎么取出索引值？\n",
    "        print()\n",
    "# 枚举:因为list其实不仅有values值，还有index索引，但for循环主要循环遍历其值，不遍历索引\n",
    "# 因此，有了枚举的方法，同时遍历两者\n",
    "# enumerate()\n",
    "pop_values_list = []  # append方法经常用于新建列表\n",
    "for index,item in enumerate(stu_id):\n",
    "    print(index,item)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f467482c",
   "metadata": {},
   "source": [
    "###          list方法总结"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "1e2825b5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------增加append--------\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "###          list方法总结\n",
    "print(\"--------增加append--------\")\n",
    "# append()方法在列表的末尾增加一个对象\n",
    "baby = []\n",
    "print(len(baby))\n",
    "baby.append(\"p\")\n",
    "baby.append(\"y\")\n",
    "baby.append(\"t\")\n",
    "baby.append(\"h\")\n",
    "baby.append(\"o\")\n",
    "baby.append(\"n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "442e742d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------删除remove、pop--------\n",
      "[1, 2, 3, 4]\n",
      "[1, 2, 4]\n"
     ]
    }
   ],
   "source": [
    "print(\"--------删除remove、pop--------\")\n",
    "# remove() 方法从列表中删除对象\n",
    "love = [1, 2, 3, 4]\n",
    "print(love)\n",
    "love.remove(3)\n",
    "print(love)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "8ee61664",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2]\n",
      "[2]\n"
     ]
    }
   ],
   "source": [
    "# pop() 方法根据对象的索引值从现有列表删除和返回一个对象\n",
    "nums.pop() # 没有指定索引值则默认删除和返回列表中最后一个对象\n",
    "print(nums)\n",
    "nums.pop(0)\n",
    "print(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "ea7aef33",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------扩展extend-----------\n",
      "[2, 3, 4]\n"
     ]
    }
   ],
   "source": [
    "print(\"--------扩展extend-----------\")\n",
    "# extend() 方法接受第二个列表，将其中的各个对象增加到现有列表；合二为一。\n",
    "nums.extend([3, 4])\n",
    "print(nums)\n",
    "nums.extend([])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "bcac13a6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------插入insert-----------\n",
      "[1, 1, 1, '哈哈哈你是猪', 1, '哈哈哈你是猪', 1, '哈哈哈你是猪', 1, '哈哈哈你是猪', 1, '哈哈哈你是猪', 1, '哈哈哈你是猪', 1, '哈哈哈你是猪', 1, '哈哈哈你是猪', 1, '哈哈哈你是猪', 1, '哈哈哈你是猪', 1, '哈哈哈你是猪', 1, '哈哈哈你是猪', 1, '哈哈哈你是猪', 2, 'two-and-a-half', 3, 4]\n",
      "[1, 1, '哈哈哈你是猪', 1, '哈哈哈你是猪', 1, '哈哈哈你是猪', 1, '哈哈哈你是猪', 1, '哈哈哈你是猪', 1, '哈哈哈你是猪', 1, '哈哈哈你是猪', 1, '哈哈哈你是猪', 1, '哈哈哈你是猪', 1, '哈哈哈你是猪', 1, '哈哈哈你是猪', 1, '哈哈哈你是猪', 1, '哈哈哈你是猪', 1, '哈哈哈你是猪', 2, 'two-and-a-half', 3, 4]\n"
     ]
    }
   ],
   "source": [
    "print(\"--------插入insert-----------\")\n",
    "# 在列表中插入一个对象\n",
    "# insert() 方法将一个对象插入到现有列表中指定索引值前面\n",
    "nums.insert(0,1) # (索引值, 对象)\n",
    "print(nums)\n",
    "# 使用insert()方法将一个对象插入现有列表任意槽\n",
    "nums.insert(2,\"哈哈哈你是猪\") # 第一个参数指示要插入到哪个索引值之前\n",
    "print(nums)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d519910",
   "metadata": {},
   "source": [
    "num是列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "a46d3d6f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-------查看len、count-------------\n",
      "列表元素个数： 4\n",
      "列表中元素出现的个数： 1\n"
     ]
    }
   ],
   "source": [
    "print(\"-------查看len、count-------------\")\n",
    "## len() 方法查看列表元素的个数\n",
    "## count() 方法查看该元素在列表出现的次数\n",
    "names=['哈哈','哇哇','嘤嘤','滴滴']\n",
    "print('列表元素个数：',len(names))\n",
    "print('列表中元素出现的个数：',names.count('滴滴'))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b610c65d",
   "metadata": {},
   "source": [
    "###          list切片总结(通过下列课本项目分析）\n",
    "对比panic.py 和 panic2.py\n",
    "panic.py 使用列表的方法来处理 而 panic2.py 用的是切片的方法\n",
    "### 列表的方法会改变一个列表的原始状态，而切片通常不会改变列表的状态"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "001e6742",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i\n",
      "i\n",
      "a\n",
      "-----------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "##          list课本自习\n",
    "# vowels.py\n",
    "# for循环加列表并开始工作\n",
    "vowels = ['a','e','i','o','u']#定义元音列表\n",
    "word = \"Milliways\"\n",
    "for letter in word:#对象是否在另一个对象中，可用\" in \"来检查\n",
    "    if letter in vowels:\n",
    "        print(letter)\n",
    "print(\"-----------------------------------------------\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "397f51c0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "# 空列表；利用len解释器确认其中没有对象\n",
    "found = []\n",
    "print(len(found))\n",
    "print(\"------------------------------------------------\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "3b0599c2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['p', 'y', 't', 'h', 'o', 'n'] 6\n"
     ]
    }
   ],
   "source": [
    "# append()方法在列表的末尾增加一个对象\n",
    "found.append(\"p\")#反复调用appen（）方法在列表末尾增加更多对象\n",
    "found.append(\"y\")\n",
    "found.append(\"t\")\n",
    "found.append(\"h\")\n",
    "found.append(\"o\")\n",
    "found.append(\"n\")\n",
    "print(found,len(found))#列表长度会增加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "71cfb880",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['p', 'y', 't', 'h', 'o', 'n', 'u'] 7\n",
      "-----------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "# 除了使用 in 操作符检查对象是否包含在一个集合中\n",
    "# 还可以使用 not in 操作符组合来检查一个集合中是否不存在某个对象\n",
    "if'u' not in found:\n",
    "    found.append('u')\n",
    "print(found,len(found))\n",
    "print(\"-----------------------------------------------\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "0b80fd77",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i\n",
      "a\n",
      "---------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "# vowels2.py（修改代码）\n",
    "vowels = ['a','e','i','o','u']\n",
    "word = \"Milliways\"\n",
    "found = []#空列表起手\n",
    "for letter in word:\n",
    "    if letter in vowels:#\n",
    "        if letter not in found:#增加这个代码来确定是否更新found元音列表\n",
    "            found.append(letter)\n",
    "for vowel in found:# 第一个for循环结束时，第二个循环开始运行，它会显示”word“中找到的元音\n",
    "    print(vowel)\n",
    "print(\"---------------------------------------------------\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "3ac8e071",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:a\n",
      "a\n",
      "---------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "# vowels3.py(增加input用户输入）\n",
    "\n",
    "vowels = ['a','e','i','o','u']\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = []#\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        if letter not in found:#增加这个代码来确定是否更新found元音列表\n",
    "            found.append(letter)\n",
    "for vowel in found:# 第一个for循环结束时，第二个循环开始运行，它会显示”word“中找到的元音\n",
    "    print(vowel)\n",
    "print(\"---------------------------------------------------\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e7525305",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4]\n",
      "[1, 2, 4]\n"
     ]
    }
   ],
   "source": [
    "# 列表删除对象\n",
    "# remove() 方法从列表中删除对象\n",
    "# remove:取一个对象值作为唯一参数\n",
    "nums = [1, 2, 3, 4]\n",
    "print(nums)\n",
    "nums.remove(3)\n",
    "print(nums) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e9cef65d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2]\n",
      "[2]\n"
     ]
    }
   ],
   "source": [
    "# 列表弹出对象\n",
    "# pop() 方法根据对象的索引值从现有列表删除和返回一个对象\n",
    "# pop：取一个可选的索引值作为参数\n",
    "nums.pop() # 没有指定索引值则默认删除和返回列表中最后一个对象\n",
    "print(nums) # [1,2]\n",
    "nums.pop(0) #0对应列表中第一个对象\n",
    "print(nums) # [2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "72f16602",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 3, 4]\n"
     ]
    }
   ],
   "source": [
    "# 用对象扩展列表\n",
    "# extend() 方法接受第二个列表，将其中的各个对象增加到现有列表；合二为一。\n",
    "# extend：取一个对象列表作为唯一参数\n",
    "nums.extend([3, 4])\n",
    "print(nums) # 将[3，4]添加到nums列表中; [2, 3, 4]\n",
    "nums.extend([])# 不会发生改变"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "fc1d6781",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4]\n",
      "[1, 2, 'two-and-a-half', 3, 4]\n",
      "----------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "# 在列表中插入一个对象\n",
    "# insert() 方法将一个对象插入到现有列表中指定索引值前面\n",
    "# insert：取一个索引值和一个对象作为参数\n",
    "nums.insert(0,1) # (索引值, 对象)\n",
    "print(nums) # [1, 2, 3, 4]\n",
    "# 使用insert()方法将一个对象插入现有列表任意槽\n",
    "nums.insert(2,\"two-and-a-half\") # 第一个参数指示要插入到哪个索引值之前\n",
    "print(nums)\n",
    "print(\"----------------------------------------------------\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "46299b16",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "['o', 'n', ' ', 't', 'a', 'p']\n",
      "on tap\n",
      "--------------------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "# panic.py\n",
    "# Don't panic! --> on tap\n",
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)\n",
    "print(phrase)\n",
    "print(plist)\n",
    "for i in range(4): # for循环删除最后一个对象四次  “nic！”\n",
    "    plist.pop() # 删除最后一个对象\n",
    "plist.pop(0) # 删除第一个对象“D”\n",
    "plist.remove(\"'\") # 删除 特殊符号\n",
    "plist.extend([plist.pop(),plist.pop()]) # 交换列表末尾两个对象 \"ont ap\"\n",
    "plist.insert(2,plist.pop(3)) # 空格字符移动到 t 前 “on tap”\n",
    "new_plist = \"\".join(plist)  # 将列表转换成字符串\n",
    "print(plist)\n",
    "print(new_plist)\n",
    "print(\"--------------------------------------------------------------\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "51fcdc88",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 'two-and-a-half', 3]\n",
      "[2, 'two-and-a-half', 3]\n"
     ]
    }
   ],
   "source": [
    "# 列表弹出对象\n",
    "# pop() 方法根据对象的索引值从现有列表删除和返回一个对象\n",
    "# pop：取一个可选的索引值作为参数\n",
    "nums.pop() # 没有指定索引值则默认删除和返回列表中最后一个对象\n",
    "print(nums) # [1,2]\n",
    "nums.pop(0) #0对应列表中第一个对象\n",
    "print(nums) # [2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "147a698d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 'two-and-a-half', 3, 3, 4]\n"
     ]
    }
   ],
   "source": [
    "# 用对象扩展列表\n",
    "# extend() 方法接受第二个列表，将其中的各个对象增加到现有列表；合二为一。\n",
    "# extend：取一个对象列表作为唯一参数\n",
    "nums.extend([3, 4])\n",
    "print(nums) # 将[3，4]添加到nums列表中; [2, 3, 4]\n",
    "nums.extend([])# 不会发生改变"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "9d410933",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 1, 1, 'two-and-a-half', 1, 'two-and-a-half', 2, 'two-and-a-half', 'two-and-a-half', 3, 3, 4]\n",
      "[1, 1, 'two-and-a-half', 1, 'two-and-a-half', 1, 'two-and-a-half', 2, 'two-and-a-half', 'two-and-a-half', 3, 3, 4]\n",
      "----------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "# 在列表中插入一个对象\n",
    "# insert() 方法将一个对象插入到现有列表中指定索引值前面\n",
    "# insert：取一个索引值和一个对象作为参数\n",
    "nums.insert(0,1) # (索引值, 对象)\n",
    "print(nums) # [1, 2, 3, 4]\n",
    "# 使用insert()方法将一个对象插入现有列表任意槽\n",
    "nums.insert(2,\"two-and-a-half\") # 第一个参数指示要插入到哪个索引值之前\n",
    "print(nums)\n",
    "print(\"----------------------------------------------------\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "d43806af",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "['o', 'n', ' ', 't', 'a', 'p']\n",
      "on tap\n",
      "--------------------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "# panic.py\n",
    "# Don't panic! --> on tap\n",
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)\n",
    "print(phrase)\n",
    "print(plist)\n",
    "for i in range(4): # for循环删除最后一个对象四次  “nic！”\n",
    "    plist.pop() # 删除最后一个对象\n",
    "plist.pop(0) # 删除第一个对象“D”\n",
    "plist.remove(\"'\") # 删除 特殊符号\n",
    "plist.extend([plist.pop(),plist.pop()]) # 交换列表末尾两个对象 \"ont ap\"\n",
    "plist.insert(2,plist.pop(3)) # 空格字符移动到 t 前 “on tap”\n",
    "new_plist = \"\".join(plist)  # 将列表转换成字符串\n",
    "print(plist)\n",
    "print(new_plist)\n",
    "print(\"--------------------------------------------------------------\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "17661d75",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5]\n",
      "[1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5, 6]\n"
     ]
    }
   ],
   "source": [
    "# 将一个列表复制到另一个列表\n",
    "# copy() 方法\n",
    "first = [1, 2, 3, 4, 5,]\n",
    "second = first\n",
    "print(second)\n",
    "second.append(6)\n",
    "print(first, second) # 这个会导致first列表和second列表同时变化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "2cca25c5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6]\n",
      "[1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5, 6, 7]\n",
      "-------------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "# copy() 方法可避免上述问题\n",
    "third = second.copy()\n",
    "print(third)\n",
    "third.append(7)\n",
    "print(second,third) # third列表多了 7\n",
    "print(\"-------------------------------------------------------\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "18a2a177",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "on tap\n"
     ]
    }
   ],
   "source": [
    "# 以下为知识盲区 995\n",
    "# panic2.py\n",
    "# Don't panic! --> on tap\n",
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)\n",
    "print(phrase)\n",
    "print(plist)\n",
    "new_phrase = (\"\".join(plist[1:3])) + \\\n",
    "    (\"\".join([plist[5], plist[4], plist[7], plist[6]]))\n",
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "bd71cc62",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "# 对比panic.py 和 panic2.py\n",
    "# panic.py 使用列表的方法来处理 而 panic2.py 用的是切片的方法\n",
    "# 列表的方法会改变一个列表的原始状态，而切片通常不会改变列表的状态\n",
    "print(\"------------------------------------------------------------\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "e98e08c4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\t M\n",
      "\t a\n",
      "\t r\n",
      "\t v\n",
      "\t i\n",
      "\t n\n",
      "------------------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "# for 循环了解列表\n",
    "# marvin.py\n",
    "paranoid_android = \"Marvin\"\n",
    "letters = list(paranoid_android)\n",
    "for i in letters:\n",
    "    print('\\t', i)  # \\t 一个tab键，四个空格\n",
    "print(\"------------------------------------------------------------\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "39ddb72c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\t M\n",
      "\t a\n",
      "\t r\n",
      "\t v\n",
      "\t i\n",
      "\t n\n",
      "\n",
      "\t\t A\n",
      "\t\t n\n",
      "\t\t d\n",
      "\t\t r\n",
      "\t\t o\n",
      "\t\t i\n",
      "\t\t d\n",
      "\n",
      "\t\t\t P\n",
      "\t\t\t a\n",
      "\t\t\t r\n",
      "\t\t\t a\n",
      "\t\t\t n\n",
      "\t\t\t o\n",
      "\t\t\t i\n",
      "\t\t\t d\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# for 循环了解切片\n",
    "# mavin2.py\n",
    "paranoid_android = \"Marvin, the Paranoid Android\"\n",
    "letters = list(paranoid_android)\n",
    "for i in letters[:6]:\n",
    "    print('\\t', i)\n",
    "print()\n",
    "for j in letters[-7:]:\n",
    "    print('\\t'*2, j)\n",
    "print()\n",
    "for k in letters[12:20]:\n",
    "    print('\\t'*3, k)\n",
    "print()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "58123a7c",
   "metadata": {},
   "source": [
    "\n",
    "列表名称\t作用\n",
    "\n",
    "append( x )\t将一个项目添加到列表的末尾\n",
    "\n",
    "extend（可迭代）\t通过附加迭代中的所有项目来扩展列表\n",
    "\n",
    "insert( i , x )\t在给定位置插入一个项目\n",
    "\n",
    "remove( x )\t从列表中删除值等于x的第一项\n",
    "\n",
    "pop( [我] )\t删除列表中给定位置的项目，并返回它\n",
    "\n",
    "clear( )\t从列表中删除所有项目\n",
    "\n",
    "index( x [ ,开始[ ,结束] ] )\t在值等于x的第一个项目的列表中返回从零开始的索引\n",
    "\n",
    "count( x )\t返回x在列表中出现的次数\n",
    "\n",
    "sort( * , key = None , reverse = False )\t对列表中的项目进行排序\n",
    "\n",
    "reverse( )\t将列表的元素原地反转\n",
    "\n",
    "copy( )\t返回列表的浅拷贝\n",
    "\n",
    ":-----------:\t-------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2d085b9",
   "metadata": {},
   "source": [
    "## 知识点_列表高级方法_copy（）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "ea724a9f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "stu_id: [101, 102, 103, 104, 101, 106, 101, 109, 101] \n",
      " stu_id_matedata: [101, 102, 103, 104, 101, 106, 101, 109, 101]\n"
     ]
    }
   ],
   "source": [
    "stu_id = [101, 102, 103, 104, 101, 106, 101, 109, 101]\n",
    "#  copy()很重要\n",
    "# 思考，赋值是真正的复制么？ 赋值(=) <=> copy? 不等价\n",
    "stu_id_matedata = stu_id\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "277be87f",
   "metadata": {},
   "source": [
    "实验1：赋值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "ecc51b4c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "stu_id: [101, 103, 104, 101, 106, 101, 109, 101] \n",
      " stu_id_matedata: [101, 103, 104, 101, 106, 101, 109, 101]\n"
     ]
    }
   ],
   "source": [
    "stu_id = [101, 102, 103, 104, 101, 106, 101, 109, 101]\n",
    "stu_id_matedata = stu_id\n",
    "stu_id.pop(1)\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11c5d1a7",
   "metadata": {},
   "source": [
    "实验2 copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "2d67e9fd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "stu_id: [101, 104, 101, 106, 101, 109, 101] \n",
      " stu_id_matedata: [101, 103, 104, 101, 106, 101, 109, 101]\n"
     ]
    }
   ],
   "source": [
    "stu_id_matedata = stu_id.copy()\n",
    "stu_id.pop(1)\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a6cddce8",
   "metadata": {},
   "source": [
    "实验3 切片实现复制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "ac76fd0d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "stu_id: [101, 101, 106, 101, 109, 101] \n",
      " stu_id_matedata: [101, 104, 101, 106, 101, 109, 101]\n"
     ]
    }
   ],
   "source": [
    "stu_id_matedata = stu_id[:]\n",
    "stu_id.pop(1)\n",
    "print(\"stu_id:\",stu_id,'\\n','stu_id_matedata:',stu_id_matedata)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "176863c8",
   "metadata": {},
   "source": [
    "## 练习"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1fe08a8",
   "metadata": {},
   "source": [
    "* 列表还支持连接等操作："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "5e1cf5bf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "squares = [1, 4, 9, 16, 25]\n",
    "squares + [36, 49, 64, 81, 100] # 两个列表链接在一起"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "155f2db5",
   "metadata": {},
   "source": [
    "* 与不可变的字符串不同，列表是可变 类型，即可以更改其内容："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "c89f6e59",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 8, 27, 64, 125]"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hm = [1, 8, 27, 65, 125]\n",
    "hm[3] = 64\n",
    "hm"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76f2dbec",
   "metadata": {},
   "source": [
    "* append() 方法在列表末尾添加新项目"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "ed95dc71",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 8, 27, 64, 125, 216, 343]"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hm.append(216)  \n",
    "hm.append(7 ** 3)  \n",
    "hm"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc7190e1",
   "metadata": {},
   "source": [
    "* 可以嵌套列表（创建包含其他列表的列表）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "aa449681",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['a', 'b', 'c'], [1, 2, 3]]"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = ['a', 'b', 'c']\n",
    "n = [1, 2, 3]\n",
    "x = [a, n]\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "91322d16",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'b', 'c']"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "d4a8ee08",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'b'"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[0][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "c15ede83",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The value of i is 65536\n"
     ]
    }
   ],
   "source": [
    "i = 256*256\n",
    "print('The value of i is', i)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
